package org.phone.manager.dao;

import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MySQLOperate {

	@Autowired
	@Qualifier("mySQLDao")
	private HibernateService dao;

	public <T> Object getEntity(Class<T> c, Integer i) {
		return dao.getCurrentSession().get(c, i);
	}

	public void saveEntity(Object o) {
		dao.getCurrentSession().save(o);
	}

	public void updateEntity(Object o) {
		dao.getCurrentSession().update(o);
	}

	public void deleteEntity(Object o) {
		dao.getCurrentSession().delete(o);
	}

	public void saveOrUpdateEntity(Object o) {
		dao.getCurrentSession().saveOrUpdate(o);
	}

	public <T> List<T> getAllEntity(Class<T> c) {
		Session session = dao.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<T> t = session.createQuery("from " + c.getName()).list();
		return t;
	}

	// 分列名查询
	public <T> List<T> getEntity(Class<T> c, String columName, String queryName) {
		Session session = dao.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<T> t = session.createQuery("from " + c.getName() + " as c where c." + columName + "=:queryName")
				.setString("queryName", queryName).list();
		return t;
	}

	public List<String> getEntity(String queryStr) {
		Session session = dao.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<String> o = session.createSQLQuery(queryStr).list();
		return o;
	}

	public <T> Object getOneEntity(Class<T> c, String columName, String queryName) {
		Session session = dao.getCurrentSession();
		@SuppressWarnings("unchecked")
		T t = (T) session.createQuery("from " + c.getName() + " as c where c." + columName + "=:queryName")
				.setString("queryName", queryName).setMaxResults(1).uniqueResult();
		return t;
	}

	public HibernateService getDao() {
		return dao;
	}
	

}